package com.tanhua.model.vo;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.testpaper.SoulOptions;
import com.tanhua.model.testpaper.SoulPaper;
import com.tanhua.model.testpaper.SoulQuestions;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class SoulPaperVo implements Serializable {
	
    private String id; // 试卷id
    private String name; // 试卷名称
    private String cover; // 封面
    private String level; // 试卷级别
    private Integer star; // 星级
    private List<SoulQuestionsVo> questions = new ArrayList<>(); // 多个试题
    private Integer isLock = 1; // 是否锁住(0-解锁 1-锁住)
    private String reportId;// 最新报告(判断是否为查看报告)


    public static List<SoulPaperVo> init(List<SoulPaper> papers, Map<Long,Long> map, Integer unLockIndex) {
        List<SoulPaperVo> vos = new ArrayList<>();
        int index = 0;
        for (SoulPaper paper : papers) {
            SoulPaperVo soulPaperVo = new SoulPaperVo();
            BeanUtil.copyProperties(paper,soulPaperVo);
            soulPaperVo.setId(paper.getId().toString());
            if(map.containsKey(paper.getId())) {
                soulPaperVo.setReportId(map.get(paper.getId()).toString());
            }
            List<SoulQuestionsVo> questionsVos = new ArrayList<>();
            for (SoulQuestions questions : paper.getQuestions()) {
                SoulQuestionsVo questionsVo = new SoulQuestionsVo();
                questionsVo.setQuestion(questions.getQuestion());
                questionsVo.setId(questions.getId().toString());
                for (SoulOptions option : questions.getOptions()) {
                    questionsVo.getOptions().add(SoulOptionsVo.init(option));
                }
                questionsVos.add(questionsVo);
            }
            soulPaperVo.setQuestions(questionsVos);

            if(unLockIndex>=index) {
                soulPaperVo.setIsLock(0);
            }
            vos.add(soulPaperVo);
            index++;
        }
        return vos;
    }

    public static SoulPaperVo init(SoulPaper paper, List<SoulQuestionsVo> list){
        SoulPaperVo soulPaperVo = new SoulPaperVo();
        BeanUtil.copyProperties(paper,soulPaperVo);
        soulPaperVo.setId(paper.getId().toString());
        if(CollUtil.isEmpty(list)){
            soulPaperVo.setQuestions(new ArrayList<>());
        }
        soulPaperVo.setQuestions(list);
        soulPaperVo.setIsLock(1);
        soulPaperVo.setReportId(null);
        return soulPaperVo;
    }
}
